package com.example.yiqinmanagersystem.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yiqinmanagersystem.entity.HeSuan;
import com.example.yiqinmanagersystem.service.HeSuanService;
import com.example.yiqinmanagersystem.vo.HeSuanVo;
import com.example.yiqinmanagersystem.vo.ResponseCode;
import com.example.yiqinmanagersystem.vo.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/*
 **@Author:youpeng
 **Created on 2022/9/18
 */
@RestController
@RequestMapping("/api")
public class HeSuanController {

    @Resource
    private HeSuanService heSuanService;

    /*
    * 获取核酸数据 带条件查询
    * */
    @GetMapping("/hesuan/loadAllHeSuan")
    public ResponseResult loadAllHeSuan(HeSuanVo heSuanVo){
        //1.条件
        IPage<HeSuan> page = new Page<>(heSuanVo.getPage(),heSuanVo.getLimit());
        QueryWrapper<HeSuan> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNoneBlank(heSuanVo.getName()),"name",heSuanVo.getName());
        //2.查库
        IPage<HeSuan> heSuanIPage = heSuanService.page(page, queryWrapper);
        if(heSuanIPage!=null){
            return new ResponseResult(ResponseCode.SUCCESS, Math.toIntExact(heSuanIPage.getTotal()),heSuanIPage.getRecords());
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * 删除核酸数据 id
    * */
    @PostMapping("/hesuan/deleteHeSuan")
    public ResponseResult deleteHeSuan(Integer id){
        boolean b = heSuanService.removeById(id);
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * 新增核酸数据
    * */
    @PostMapping("/hesuan/addHeSuan")
    public ResponseResult addHeSuan(HeSuan heSuan){
        boolean b = heSuanService.save(heSuan);
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
     * 删除核酸数据
     * */
    @PostMapping("/hesuan/updateHeSuan")
    public ResponseResult updateHeSuan(HeSuan heSuan){
        boolean b = heSuanService.updateById(heSuan);
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }
}
